home *** CD-ROM | disk | FTP | other *** search
/ PC Elektro 3 / PC-Elektro-3-cd1.bin / KBan 2.0 / KBANSRC.LZH / SRC / PROG / XY.H < prev   
Encoding:
C/C++ Source or Header  |  1997-08-17  |  3.3 KB  |  136 lines

  1. /*
  2.  * a header of the class XY
  3.  * Copyright (C) 1996, 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
  4.  */
  5.  
  6. #ifndef _XY_H_
  7. #define _XY_H_
  8.  
  9. #include <math.h>
  10. #include <stdlib.h>
  11.  
  12. #include "common/base.h"
  13.  
  14. typedef int XYT;
  15.  
  16. class XY {
  17.   XYT m_x, m_y;
  18. public:
  19.   XY()
  20.     : m_x(0),
  21.       m_y(0) {}
  22.   XY(const XY& rval)
  23.     : m_x(rval.m_x),
  24.       m_y(rval.m_y) {}
  25.   XY(XYT x, XYT y)
  26.     : m_x(x),
  27.       m_y(y) {}
  28.   void  set_x(XYT x) { m_x = x; }
  29.   void  set_y(XYT y) { m_y = y; }
  30.   void  set(XYT x, XYT y) {
  31.     set_x(x);
  32.     set_y(y);
  33.   }
  34.   XYT   x() const { return m_x; }
  35.   XYT   y() const { return m_y; }
  36.   void  operator=(const XY& src) {
  37.     m_x = src.x();
  38.     m_y = src.y();
  39.   }
  40.   int  operator==(const XY& r) const {
  41.     const XY& l = *this;
  42.     return (l.x() == r.x()) && (l.y() == r.y());
  43.   }
  44.   int  operator!=(const XY& r) const {
  45.     const XY& l = *this;
  46.     return (l.x() != r.x()) || (l.y() != r.y());
  47.   }
  48.   friend XY operator+(const XY& l, const XY& r) {
  49.     return XY(l.x() + r.x(), l.y() + r.y());
  50.   }
  51.   friend XY operator+(const XY& l, XYT d) {
  52.     return XY(l.x() + d, l.y() + d);
  53.   }
  54.   XY operator+=(const XY& v) {
  55.     m_x += v.x();
  56.     m_y += v.y();
  57.     return *this;
  58.   }
  59.   XY operator+=(XYT d) {
  60.     m_x += d;
  61.     m_y += d;
  62.     return *this;
  63.   }
  64.   friend XY operator-(const XY& l) {
  65.     return XY(- l.x(), - l.y());
  66.   }
  67.   friend XY operator-(const XY& l, const XY& r) {
  68.     return XY(l.x() - r.x(), l.y() - r.y());
  69.   }
  70.   friend XY operator-(const XY& l, XYT d) {
  71.     return XY(l.x() - d, l.y() - d);
  72.   }
  73.   XY operator-=(const XY& v) {
  74.     m_x -= v.m_x;
  75.     m_y -= v.m_y;
  76.     return *this;
  77.   }
  78.   XY operator-=(XYT d) {
  79.     m_x -= d;
  80.     m_y -= d;
  81.     return *this;
  82.   }
  83.   friend XY operator*(const XY& l, double r) {
  84.     return XY(XYT(double(l.x()) * r), XYT(double(l.y()) * r));
  85.   }
  86.   friend XY operator/(const XY& l, int r) {
  87.     return XY(XYT(l.x() / r), XYT(l.y() / r));
  88.   }
  89.   friend XY operator/(const XY& l, double r) {
  90.     return XY(XYT(double(l.x()) / r), XYT(double(l.y()) / r));
  91.   }
  92.   friend int operator<(const XY& l, const XY& r) {
  93.     return (l.m_x < r.m_x) && (l.m_y < r.m_y);
  94.   }
  95.   friend int operator<=(const XY& l, const XY& r) {
  96.     return (l.m_x <= r.m_x) && (l.m_y <= r.m_y);
  97.   }
  98.   friend int operator>(const XY& l, const XY& r) {
  99.     return (l.m_x > r.m_x) && (l.m_y > r.m_y);
  100.   }
  101.   friend int operator>=(const XY& l, const XY& r) {
  102.     return (l.m_x >= r.m_x) && (l.m_y >= r.m_y);
  103.   }
  104.   friend int compare_distance_lt(const XY& p, const XY& q, XYT d);
  105.   friend double get_distance(const XY& p, const XY& q) {
  106.     double dx = p.x() - q.x();
  107.     double dy = p.y() - q.y();
  108.     return sqrt(dx * dx + dy * dy);
  109.   }
  110.   double  get_radian(const XY& p) const;
  111.   void   rotate(const XY& src, double rad);
  112.   void  rotate_90() {
  113.     *this = XY(-y(), x());
  114.   }
  115.   bool is_in_box(const XY& p, const XY& q) const;
  116.  
  117.   friend XY get_max(const XY& p, const XY& q) {
  118.     return XY(maximum(p.x(), q.x()), maximum(p.y(), q.y()));
  119.   }
  120.   friend XY get_min(const XY& p, const XY& q) {
  121.     return XY(minimum(p.x(), q.x()), minimum(p.y(), q.y()));
  122.   }
  123. };
  124.  
  125. #include "record.h"
  126.  
  127. inline RECORD_STREAM& operator<<(RECORD_STREAM& rec, const XY& target)
  128. {
  129.   char rec_str[100];
  130.   sprintf(rec_str, "(%d, %d)", target.x(), target.y());
  131.   rec << rec_str;
  132.   return rec;
  133. }
  134.  
  135. #endif /* _XY_H_ */
  136.